package com.fuqi.android.zhongcai.base.view.activity;


import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.view.adapter.BaseRecyclerViewAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.TrainingAdapter;
import com.fuqi.android.zhongcai.base.request.api.TrainListApi;
import com.fuqi.android.zhongcai.base.request.result.TrainListResult;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.LogUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import butterknife.Bind;

public class TrainingActivity extends BaseActivity {

    @Bind(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private ArrayList<TrainListResult> listData;
    private TrainingAdapter trainingAdapter;


    private void initRecycleView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                currentPage = 1;
                new Handler().postDelayed(new Runnable(){
                    public void run() {

                        listData.clear();
//                        orderList.clear();
//                        RequestManager.getInstance().call(new OrderListApi(new OrderListApi.OrderListParams(orderType,
//                                String.valueOf(showCount),
//                                String.valueOf(currentPage)), respOrderListener, errorListener));
                        trainingAdapter.notifyDataSetChanged();
                        mRecyclerView.refreshComplete();

                    }

                }, 1000);            //refresh data here
            }

            @Override
            public void onLoadMore() {




            }
        });

        listData = new ArrayList<TrainListResult>();
        trainingAdapter = new TrainingAdapter(listData);

        mRecyclerView.setAdapter(trainingAdapter);
//        mRecyclerView.setRefreshing(true);
        trainingAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<TrainListResult>() {
            @Override
            public void onItemClick(View view, int position, TrainListResult data) {

                Bundle bundle = new Bundle();
                bundle.putString("data",GsonHelper.writeValue(data));
                ActivityUtils.startActivityNeedLogin(getApplicationContext(),TrainingDetailActivity.class,bundle);

            }
        });

    }

    @Override
    public void initView() {

        initRecycleView();

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

        titleBar.setTitle("培训交流");

        RequestManager.getInstance().call(new TrainListApi(new TrainListApi.TrainListParams(
                String.valueOf(showCount),
                String.valueOf(currentPage)), respTrainListener, errorListener));

    }


    Response.Listener<TrainListResult[]> respTrainListener = new Response.Listener<TrainListResult[]>() {

        @Override
        public void onResponse(TrainListResult[] response) {

            if (HandleCode.requestSuccess()) {
                for(int i = 0; i < response.length ;i++) {

                    listData.add(response[i]);
                }

                trainingAdapter.notifyDataSetChanged();
                mRecyclerView.refreshComplete();
                LogUtils.i( "response TrainListResult: " + GsonHelper.writeValue(response));

            } else {

            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_training;
    }
}
